package com.example.appframework.login.modifypassword;

import androidx.lifecycle.ViewModelProviders;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.example.appframework.R;
import com.example.appframework.login.LoginMainActivity;
import com.example.appframework.mysql.DBConnection;

public class ModifyPasswordFragment extends Fragment {

    private ModifyPasswordViewModel mViewModel;
    EditText modifyUsername, modifyOldPassword, modifyNewPassword, modifyNewPassword2;

    public static ModifyPasswordFragment newInstance() {
        return new ModifyPasswordFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.modify_password_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(ModifyPasswordViewModel.class);
        // TODO: Use the ViewModel

        modifyUsername = getView().findViewById(R.id.modifyUsername);
        modifyOldPassword = getView().findViewById(R.id.modifyOldPassword);
        modifyNewPassword = getView().findViewById(R.id.modifyNewPassword);
        modifyNewPassword2 = getView().findViewById(R.id.modifyNewPassword2);

        setMyType(getActivity(), 1);

        getView().findViewById(R.id.buttonModifyConfirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final  String username = modifyUsername.getText().toString();
                final  String oldpassword = modifyOldPassword.getText().toString();
                final  String newpassword = modifyNewPassword.getText().toString();
                final  String newpassword2 = modifyNewPassword2.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(oldpassword)||TextUtils.isEmpty(newpassword)||TextUtils.isEmpty(newpassword2)) {
                    Toast.makeText(getContext(), "Can't be empty!",Toast.LENGTH_SHORT).show();
                    Log.i("show", "empty!");
                }
                else if(!checkPassword(newpassword)){
                    Toast.makeText(getContext(), "The new password is malformed!",Toast.LENGTH_SHORT).show();
                }
                else if(!newpassword.equals(newpassword2)){
                    Toast.makeText(getContext(), "Two new passwords are unequal!",Toast.LENGTH_SHORT).show();
                    Log.i("show", "unequal!");
                }
                else{
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if(DBConnection.query(username, oldpassword)){
                                DBConnection.update(username,newpassword);
                                Log.i("show", "update");
                                Looper.prepare();
                                Toast.makeText(getContext(), "Trying to modify!", Toast.LENGTH_SHORT).show();
                                NavController navController = Navigation.findNavController(getActivity(),R.id.fragmentLogin);
                                navController.navigate(R.id.action_modifyPasswordFragment4_to_loginMethodFragment);
                                Looper.loop();
                            }
                            else{
                                Looper.prepare();
                                Toast.makeText(getContext(), "Your information is wrong!", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                                Log.i("show", "failed");
                            }
                        }
                    }).start();
                }
            }
        });
    }

    private Boolean checkPassword(String str){
        Boolean f0 = false, f1 = false, f2 = false, f3 = false, f4 = false;
        f0 = str.length() >= 6 && str.length() <= 18;
        for(int i=0; i < str.length(); i++){
            char c = str.charAt(i);
            if(Character.isDigit(c)){
                f1 = true;
            }
            else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
                f2 = true;
            }
            else if((c>='!'&&c<='/')||(c>=':'&&c<='@')||(c>='['&&c<='`')||(c>='{'&&c<='~')){
                f3 = true;
            }
            else {
                f4 = true;
            }
        }
        return f0 && (f1 & f2 & f3) && !f4;
    }

    private void setMyType(Activity activity, int t) {
        super.onAttach(activity);
        ((LoginMainActivity) activity).setType(t);
    }

}